<?php
/*
 * 404后台管理文件
 * @author     Mark <zhaody901@126.com>
 * @copyright  Copyright (c) 2013 lemon team
 * @version    $Id: 774ceb0cad9def2984cbf11c1d5dbada2231a68e $
 * @time       14-2-7 上午10:02
 */
defined('IN_IXDCW') or exit('Access Denied');
$menus = array (
	array('404日志', '?file='.$file),
	array('清空日志', '?file='.$file.'&action=truncate', 'onclick="if(!confirm(\'确定要清空所有404日志吗？\')) return false"'),
);

switch($action) {
	case 'delete':
		$itemid or msg('请选择记录');
		$ids = is_array($itemid) ? implode(',', $itemid) : $itemid;
		$db->query("DELETE FROM {$db->getPre()}404 WHERE itemid IN ($ids)");
		dmsg('删除成功', $forward);
	break;
	case 'truncate':
		$db->query("TRUNCATE TABLE {$db->getPre()}404");
		dmsg('清理成功', $forward);
	break;
	default:
		require DT_ROOT.'/file/config/robot.inc.php';
		$sfields = array(
			'url'=>'网址',
			'robot'=>'搜索引擎',
			'username'=>'会员',
			'ip'=>'IP'
		);
		isset($fields) && isset($sfields[$fields]) or $fields = 0;
		$ip = isset($ip) ? $ip : '';
		$robot = isset($robot) ? $robot : '';
		$username = isset($username) ? $username : '';
		$fromdate = isset($fromdate) ? $fromdate : '';
		$fromtime = is_date($fromdate) ? strtotime($fromdate.' 0:0:0') : 0;
		$todate = isset($todate) ? $todate : '';
		$totime = is_date($todate) ? strtotime($todate.' 23:59:59') : 0;
		$condition = '1';
		if($kw) $condition .= " AND $fields LIKE '%$kw%'";
		if($fromtime) $condition .= " AND addtime>$fromtime";
		if($totime) $condition .= " AND addtime<$totime";
		if($ip) $condition .= " AND ip='$ip'";
		if($robot) $condition .= " AND robot='$robot'";
		if($username) $condition .= " AND username='$username'";	
		$r = $db->getOne("SELECT COUNT(*) AS num FROM {$db->getPre()}404 WHERE $condition");
		$pages = pages($r['num'], $page, $pagesize);		
		$lists = array();
		$result = $db->query("SELECT * FROM {$db->getPre()}404 WHERE $condition ORDER BY itemid DESC LIMIT $offset,$pagesize");
		while($r = $db->fetchArray($result)) {
			$tmp = parse_url($r['url']);
			$r['durl'] = Str::cut(basename($r['url']), 30, '...');
			$r['addtime'] = timetodate($r['addtime'], 6);
			$lists[] = $r;
		}

		array_unshift($ROBOT, '搜索引擎');

		include tpl('404');
	break;
}